package com.nnnu.wsnackshop.controller.admin;

import com.nnnu.wsnackshop.controller.base.BaseController;
import com.nnnu.wsnackshop.result.Result;
import com.nnnu.wsnackshop.service.IDashboardService;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理后台仪表盘数据控制器
 */
@RestController
@RequestMapping("/api/admin/dashboard")
@RequiredArgsConstructor
public class DashboardController extends BaseController {


    private final IDashboardService dashboardService;

    /**
     * 获取仪表盘统计数据
     * 
     * @return 统计数据，包括今日订单数、总用户数、总收入和优惠券使用数等
     */
    @GetMapping("/stats")
    @RequiresPermissions("dashboard:view")
    public Result<?> getStats() {
        return Result.success(dashboardService.getDashboardStats());
    }

    /**
     * 获取销售趋势数据
     * 
     * @param timeRange 时间范围：'day', 'week', 'month', 'year'
     * @return 销售趋势数据
     */
    @GetMapping("/sales-trend")
    @RequiresPermissions("dashboard:view")
    public Result<?> getSalesTrend(@RequestParam(defaultValue = "week") String timeRange) {
        return Result.success(dashboardService.getSalesTrend(timeRange));
    }

    /**
     * 获取销售分类占比数据
     * 
     * @param timeRange 时间范围：'day', 'week', 'month', 'year'
     * @return 各分类销售占比数据
     */
    @GetMapping("/category-sales")
    @RequiresPermissions("dashboard:view")
    public Result<?> getCategorySales(@RequestParam(defaultValue = "week") String timeRange) {
        return Result.success(dashboardService.getCategorySales(timeRange));
    }

    /**
     * 获取最近订单数据
     * 
     * @param limit 获取的订单数量
     * @return 最近订单列表
     */
    @GetMapping("/recent-orders")
    @RequiresPermissions("dashboard:view")
    public Result<?> getRecentOrders(@RequestParam(defaultValue = "5") int limit) {
        return Result.success(dashboardService.getRecentOrders(limit));
    }

    /**
     * 获取热门商品数据
     * 
     * @param limit 获取的商品数量
     * @return 热门商品列表
     */
    @GetMapping("/hot-products")
    @RequiresPermissions("dashboard:view")
    public Result<?> getHotProducts(@RequestParam(defaultValue = "5") int limit) {
        return Result.success(dashboardService.getHotProducts(limit));
    }

    /**
     * 获取用户增长数据
     * 
     * @param timeRange 时间范围：'week', 'month', 'year'
     * @return 用户增长的时间序列数据
     */
    @GetMapping("/user-growth")
    @RequiresPermissions("dashboard:view")
    public Result<?> getUserGrowth(@RequestParam(defaultValue = "month") String timeRange) {
        return Result.success(dashboardService.getUserGrowth(timeRange));
    }
}